package com.monlyu.util;

/**
 * 
 * @author monlyu
 * @since 2009-12-15
 */
public class HashUtil {
	/**
	 * 算某些对象的hash值
	 * 
	 * @param args 多个连续参数
	 * @return 算出来的散列值
	 */
	public static int hash(Object... args) {
		int default_key = 33;
		for (Object object : args) {
			int i;
			if (object instanceof Object[]) {
				i = hash((Object[]) object);
			} else {
				i = object == null ? 0 : object.hashCode();
			}
			default_key = default_key << 2 + i;
		}
		return default_key;
	}

	/**
	 * 判断两个对象是否相同
	 * 比较内部的各个属性
	 * 
	 * @return 2个对象是否相同 true 相同
	 */
	public static boolean equals(Object o1, Object o2) {
		if (o1 == null) {
			return o2 == null;
		}
		if (o1 instanceof Object[]) {
			if (o2 instanceof Object[]) {
				Object[] ary1 = (Object[]) o1;
				Object[] ary2 = (Object[]) o2;
				if (ary1.length == ary2.length) {
					for (int i = 0; i < ary1.length; i++) {
						if (!HashUtil.equals(ary1[i], ary2[i])) {
							return false;
						}
					}
					return true;
				}
			}
			return false;
		}
		return o1.equals(o2);
	}
}
